package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.orewar.CrossOreWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_CROSS_OREWAR_ROBPIT, desc = "占领跨服盐场坑位")
public class CrossRobPitCmd extends CrossUnionCommand {

    private long userId;
    private int activityId;
    private int pitId;
    private long pitOwnerUserId;
    private String patronsInfoStr;
    private UserBaseInfo userBaseInfo;
    private int beautySkillAddition;
    private int patronsSkillAddition;
    private int refugeCount;//避难所有多少人，要和跨服坑位一起判断是否超过可以占领上限

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OreWarProto.CrossOrePitRobReqMsg reqMsg = OreWarProto.CrossOrePitRobReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        pitId = reqMsg.getPitId();
        pitOwnerUserId = reqMsg.getPitOwnerUserId();
        patronsInfoStr = reqMsg.getPatronsInfoStr();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
        beautySkillAddition = reqMsg.getBeautySkillAddition();
        patronsSkillAddition = reqMsg.getPatronsSkillAddition();
        refugeCount = reqMsg.getRefugeCount();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        OreWarProto.CrossOrePitRobRespMsg.Builder respMsg = OreWarProto.CrossOrePitRobRespMsg.newBuilder();
        if(!CrossOreWarMgr.isActivityActive(activityId,groupId)){
            respMsg.setRet(GameErrorCode.E_ORE_ACTIVITY_NO_IN_DAY_TIME);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ORE_WAR_ROBPIT, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
            return;
        }
        respMsg = CrossOreWarMgr.robPit(
                userId,userBaseInfo,activityId,pitId,pitOwnerUserId,patronsInfoStr,beautySkillAddition,patronsSkillAddition,refugeCount,groupId);
        long nowTime = System.currentTimeMillis();
        log.info("userId:{},cross ore war battle cost time:{}", userId ,System.currentTimeMillis() - nowTime);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ORE_WAR_ROBPIT, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }

}
